package com.raspberryCar.model;

import java.io.IOException;
import java.math.BigDecimal;

import com.pi4j.gpio.extension.pca.PCA9685GpioProvider;
import com.pi4j.gpio.extension.pca.PCA9685Pin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;

/**
 * 舵机控制基类，主要初始化一些参数
 * @author huyong
 *
 */
public class ServoMotor {
	static PCA9685GpioProvider gpioProvider;
	GpioController gpio = null;
	public ServoMotor() {
		if (gpio == null) {
			gpio = GpioFactory.getInstance();
		}
		BigDecimal frequency = new BigDecimal("48.828");
		BigDecimal frequencyCorrectionFactor = new BigDecimal("1.0578");
		I2CBus bus;
		try {
			bus = I2CFactory.getInstance(I2CBus.BUS_1);
			gpioProvider = new PCA9685GpioProvider(bus, 0x40, frequency,
					frequencyCorrectionFactor);
			// Define outputs in use for this example
			gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_02, "Pulse 02");
			// Reset outputs
			gpioProvider.setAlwaysOff(PCA9685Pin.PWM_02);
			gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_00, "Pulse 00");
			gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_01, "Pulse 01");
			// Reset outputs
			gpioProvider.setAlwaysOff(PCA9685Pin.PWM_00);
			gpioProvider.setAlwaysOff(PCA9685Pin.PWM_01);
		} catch (UnsupportedBusNumberException | IOException e) {
			e.printStackTrace();
		}
	}
	public GpioController getGpioController(){
		return gpio;
	}
}
